package com.xunwulian.base.util;

/**
 * Created by liyankai on 2016/9/12.
 */
public class TextUtil {

    public static String encodeHtml(String origin) {
        origin = origin.replaceAll(">", "&gt;");
        origin = origin.replaceAll("<", "&lt;");
        origin = origin.replaceAll(" ", " &nbsp;");
        origin = origin.replaceAll(" ", " &nbsp;");
        origin = origin.replaceAll("\"", "&quot;");
        origin = origin.replaceAll("\'", "&#39;");
        origin = origin.replaceAll("\n", "<br/> ");
        return origin;
    }

    public static String decodeHtml(String origin) {
        origin = origin.replaceAll("&gt;", ">");
        origin = origin.replaceAll("&lt;", "<");
        origin = origin.replaceAll("&nbsp;", " ");
        origin = origin.replaceAll(" &nbsp;", " ");
        origin = origin.replaceAll("&quot;", "\"");
        origin = origin.replaceAll("&#39;", "\'");
        origin = origin.replaceAll("<br/> ", "\n");
        return origin;
    }

    public static boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }

    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}
